﻿package game 
{
	import com.sickworks.components.DateChooserComp;
	import flash.events.Event;
	import game.exchangeddata.HashTable;
	import game.exchangeddata.ReportInfo;
	
	import fl.controls.TextInput;
	
	import flash.display.MovieClip;
	import fl.controls.ButtonLabelPlacement
	import flash.display.SimpleButton;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import game.events.EventData;
	import common.Utility;
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)
	 */
	public class GameReportPanel extends MovieClip
	{
		public var dateChoose:DateChooserComp;
		public var dateFromIcon:MovieClip;
		public var dateToIcon:MovieClip;
		public var txtDateFromTi:TextInput;
		public var txtDateToTi:TextInput;
		public var movMaskWinLose:MovieClip;
		public var btnSearchReport:SimpleButton;
		public var btnCancelReport:SimpleButton;
		
		public var txtReportTitleTf:TextField;
		public var txtDatefromTf:TextField;
		public var txtDateToTf:TextField;
		public var txtOkTf:TextField;
		public var txtCloseTf:TextField;
		public var txtBetAmountTf:TextField;
		public var txtWinloseTf:TextField;
		public var txtRollingAmountTf:TextField;
		public var txtComTypeTf:TextField;
		public var txtComRateTf:TextField;
		public var txtComAmountTf:TextField;
		public var txtTotalTf:TextField;
		
		public var txtValueBetAmountTf:TextField;
		public var txtValueWinloseTf:TextField;
		public var txtValueRollingAmountTf:TextField;
		public var txtValueComTypeTf:TextField;
		public var txtValueComRateTf:TextField;
		public var txtValueComAmountTf:TextField;
		public var txtValueTotalTf:TextField;
		
		private var currentIconDate:MovieClip;
		private var dateFrom:Array;
		private var dateTo:Array;
		public function GameReportPanel() 
		{
			hide();
			dateFrom = new Array();
			dateTo = new Array();
			this.dateChoose.visible = false;
			this.txtOkTf.mouseEnabled = false;
			this.txtCloseTf.mouseEnabled = false;
			
			this.dateFromIcon.addEventListener(MouseEvent.CLICK, dateIconClickHdl);
			this.dateToIcon.addEventListener(MouseEvent.CLICK, dateIconClickHdl);
			this.dateChoose.addEventListener(EventData.SELECTION_CHANGED, dateChooseClickHdl);
			this.btnSearchReport.addEventListener(MouseEvent.CLICK, searchReportHdl);
			this.btnCancelReport.addEventListener(MouseEvent.CLICK, cancelReportHdl);
			
		}
		public function show():void {
			this.visible = true;
		}
		public function hide():void {
			this.visible = false;
		}
		private function searchReportHdl(e:Event):void 
		{
			//trace("searchReportHdl");
			if (txtDateFromTi.text == "" || txtDateToTi.text == "")
			{	
				dispatchEvent(new Event(EventData.SHOW_ERR_DATE_NULL, true));
				return;
			}
			var dateF:Date = new Date(dateFrom[0], dateFrom[1], dateFrom[2]);
			var dateT:Date = new Date(dateTo[0], dateTo[1], dateTo[2]);
			if(dateF < dateT)
				dispatchEvent(new Event(EventData.GET_REPORT, true));
			else 
				dispatchEvent(new Event(EventData.SHOW_ERR_CHOOSE_DATE, true));
		}
		
		private function cancelReportHdl(e:Event):void 
		{
			hide();
		}
		
			
		private function dateChooseClickHdl(e:Event):void 
		{
			
			this.dateChoose.visible = false;
			var dateSelect:String = e.target.selectedDates[0];
			var arrayDate:Array = dateSelect.split(" ");
			dateSelect = arrayDate[2] + "/" + arrayDate[1] + "/" +  arrayDate[5];
			if (currentIconDate == dateToIcon)
			{	
				txtDateToTi.text = dateSelect;
				dateTo[0] = arrayDate[5];
				dateTo[1] = Utility.getMonthNumber(arrayDate[1]);
				dateTo[2] = arrayDate[2];
				dispatchEvent(new Event(EventData.UPDATE_DATE_TO, true));
			}
			else if (currentIconDate == dateFromIcon)
			{
				txtDateFromTi.text = dateSelect;
				dateFrom[0] = arrayDate[5];
				dateFrom[1] =  Utility.getMonthNumber(arrayDate[1]);
				dateFrom[2] = arrayDate[2] ;
				dispatchEvent(new Event(EventData.UPDATE_DATE_FROM, true));
			}
		}
		
		private function dateIconClickHdl(e:MouseEvent):void 
		{
			if(e.currentTarget == dateToIcon)
			{
				this.dateChoose.y = this.dateToIcon.y - 10;
				this.dateChoose.visible = true;
				currentIconDate = dateToIcon;
			}
			else if (e.currentTarget == dateFromIcon)
			{
				this.dateChoose.y = this.dateFromIcon.y - 10;
				this.dateChoose.visible = true;
				currentIconDate = dateFromIcon;
			}
		}
		
		public function initText(hashTableInfo:HashTable):void {
				this.txtReportTitleTf.text = hashTableInfo.lookupString("reportMsg");
				this.txtDatefromTf.text = hashTableInfo.lookupString("dFromMsg");
				this.txtDateToTf.text = hashTableInfo.lookupString("dToMsg");
				this.txtBetAmountTf.text = hashTableInfo.lookupString("betAmountMsg");
				this.txtWinloseTf.text = hashTableInfo.lookupString("winMsg")+":";
				this.txtRollingAmountTf.text = hashTableInfo.lookupString("rollingMsg");
				this.txtComTypeTf.text = hashTableInfo.lookupString("cTypeMsg");
				this.txtComRateTf.text = hashTableInfo.lookupString("cRateMsg");
				this.txtComAmountTf.text = hashTableInfo.lookupString("cAmountMsg");
				this.txtTotalTf.text = hashTableInfo.lookupString("tAmountMsg");
				this.txtOkTf.text = hashTableInfo.lookupString("btnOK");
				this.txtCloseTf.text = hashTableInfo.lookupString("btnclose");
				
		}
		/**
		 * @author	Bamboo
		 * @param	dFrom
		 * @param	dTo
		 */
		public function updateDate(dFrom:String, dTo:String):void {
			this.txtDateFromTi.text = dFrom;
			this.txtDateToTi.text = dTo;
			var tempFrom:Array = dFrom.split("/");
			this.dateFrom[0] = tempFrom[2];
			this.dateFrom[1] = Utility.getMonthNumber(tempFrom[1]);
			this.dateFrom[2] = tempFrom[0];
			
			var tempTo:Array = dTo.split("/");
			this.dateTo[0] = tempTo[2];
			this.dateTo[1] = Utility.getMonthNumber(tempTo[1]);
			this.dateTo[2] = tempTo[0];
		}
		/**
		 * @author	Bamboo
		 * @param	info
		 */
		public function updateValueReport(info:Object):void {
			var report:ReportInfo = ReportInfo(info);
			this.txtValueBetAmountTf.text = report.valueBetAmount;
			this.txtValueWinloseTf.text = report.valueWinlose;
			this.txtValueRollingAmountTf.text = report.valueRollingAmount;
			this.txtValueComTypeTf.text = report.valueComType;
			this.txtValueComRateTf.text = report.valueComRate;
			this.txtValueComAmountTf.text = report.valueComAmount;
			this.txtValueTotalTf.text = report.valueTotal;
			if (report.valueRollingAmount == "0")
				this.movMaskWinLose.visible = true;
			else	
				this.movMaskWinLose.visible = false;
		}
		
	}

}